//
//  RegisterService.swift
//  GreenDeal
//
//  Created by Moennig Q on 22/2/2023.
//

import UIKit
import Alamofire
import HandyJSON
import SwiftyJSON
class RegisterService: BaseService {
    //MARK: -检查AccreditedNumber
    func checkAccreditedNumberWhenRegister(_ accreditationNumber: String){
        let url : String = APIConfig.getRequestAPIForV2(APIConfig.CheckFieldWhenRegisterAPI)
        
        let params : [String: String] = ["field" : "accredited_number", "value": accreditationNumber]
        let alRequest = ALRequest.init(mainURL: url, method: .get, headers: RequestConstant.AuthLoginOtherParam, paramDict: params, successBlock: { (obj, _ ) -> Void in
            self.returnBlock(obj, nil)
        }) {(code, obj) -> Void in
            self.errorBlock?(code, obj)
        }
        alRequest.startRequest()
    }
    //MARK: -检查email
    func checkEmailWhenRegister(_ email: String){
        let url : String = APIConfig.getRequestAPIForV2(APIConfig.CheckFieldWhenRegisterAPI)
        
        let params : [String: String] = ["field" : "email", "value": email]
        let alRequest = ALRequest.init(mainURL: url, method: .get, headers: RequestConstant.AuthLoginOtherParam, paramDict: params, successBlock: { (obj, _ ) -> Void in
            self.returnBlock(obj, nil)
        }) {(code, obj) -> Void in
            self.errorBlock?(code, obj)
        }
        alRequest.startRequest()
    }
    //MARK: -注册第一个界面点击continue是调用校验
    func checkAccreditedNumberAndNameWhenRegisterContinue(_ model: RegisterModel){
        let url : String = APIConfig.getRequestAPIForV2(APIConfig.CheckAccreditedNumberAndNameWhenRegisterAPI)
        
        let params : [String: String] = ["accredited_number" : model.accredited_number, "first_name": model.first_name, "last_name": model.last_name]
        let alRequest = ALRequest.init(mainURL: url, method: .get, headers: RequestConstant.AuthLoginOtherParam, paramDict: params, successBlock: { (obj, _ ) -> Void in
            self.returnBlock(obj, nil)
        }) {(code, obj) -> Void in
            self.errorBlock?(code, obj)
        }
        alRequest.startRequest()
    }
    //MARK: -submit register
    func submitRegisterInfo(_ model: RegisterModel){
        let url : String = APIConfig.getRequestAPIForV2(APIConfig.SubmitRegisterInfoAPI)
        
        let params : [String: String] = ["info": self.getJSONString(model)]
            
        let alRequest = ALRequest.init(mainURL: url, method: .post, headers: RequestConstant.AuthLoginOtherParam, paramDict: params, successBlock: { (obj, _ ) -> Void in
            self.returnBlock(obj, nil)
        }) {(code, obj) -> Void in
            self.errorBlock?(code, obj)
        }
        alRequest.startRequest()
    }
    private func getJSONString(_ model: RegisterModel)->String{
        let addressTypeStr = "\"address_type\" : " + "\"\(model.physicalAndPOBoxAddressViewModel.address_type.rawValue)\""
        var resultStr = ""
        if  model.physicalAndPOBoxAddressViewModel.address_type == .physical {
            resultStr = "{\"email\" : " + "\"\(model.email)\",\"password\" : " + "\"\(model.password)\",\"password_confirmation\" : " + "\"\(model.password_confirmation)\",\"first_name\" : " + "\"\(model.first_name)\",\"last_name\" : " + "\"\(model.last_name)\",\"phone\" : " + "\"\(model.phone)\",\"unit_type\" : " + "\"\(model.physicalAndPOBoxAddressViewModel.physicalAddress.unit_type)\",\"unit_number\" : " + "\"\(model.physicalAndPOBoxAddressViewModel.physicalAddress.unit_number)\",\"street_number\" : " + "\"\(model.physicalAndPOBoxAddressViewModel.physicalAddress.street_number)\",\"street_type\" : " + "\"\(model.physicalAndPOBoxAddressViewModel.physicalAddress.street_type)\",\"street_name\" : " + "\"\(model.physicalAndPOBoxAddressViewModel.physicalAddress.street_name)\",\"postcode\" : " + "\"\(model.physicalAndPOBoxAddressViewModel.physicalAddress.post_code)\",\"suburb\" : " + "\"\(model.physicalAndPOBoxAddressViewModel.physicalAddress.suburb)\",\"state\" : " + "\"\(model.physicalAndPOBoxAddressViewModel.physicalAddress.state)\",\"electrician_licenses\" : " + "\"\(model.electrician_licenses)\",\"accredited_number\" : " + "\"\(model.accredited_number)\",\"delivery_type\" : " + "\"\(model.physicalAndPOBoxAddressViewModel.delivery_type)\",\"delivery_number\" : " + "\"\(model.physicalAndPOBoxAddressViewModel.delivery_number)\",\"installer\" : " + "\"\((model.installer ? "true" : "false"))\",\"designer\" : " + "\"\((model.designer ? "true" : "false"))\",\"electrician\" : " + "\"\((model.electrician ? "true" : "false"))\"," + addressTypeStr + "}"
        }else{
            resultStr = "{\"email\" : " + "\"\(model.email)\",\"password\" : " + "\"\(model.password)\",\"password_confirmation\" : " + "\"\(model.password_confirmation)\",\"first_name\" : " + "\"\(model.first_name)\",\"last_name\" : " + "\"\(model.last_name)\",\"phone\" : " + "\"\(model.phone)\", \"postcode\" : " + "\"\(model.physicalAndPOBoxAddressViewModel.post_code)\",\"suburb\" : " + "\"\(model.physicalAndPOBoxAddressViewModel.suburb)\",\"state\" : " + "\"\(model.physicalAndPOBoxAddressViewModel.state)\",\"electrician_licenses\" : " + "\"\(model.electrician_licenses)\",\"accredited_number\" : " + "\"\(model.accredited_number)\",\"delivery_type\" : " + "\"\(model.physicalAndPOBoxAddressViewModel.delivery_type)\",\"delivery_number\" : " + "\"\(model.physicalAndPOBoxAddressViewModel.delivery_number)\",\"installer\" : " + "\"\((model.installer ? "true" : "false"))\",\"designer\" : " + "\"\((model.designer ? "true" : "false"))\",\"electrician\" : " + "\"\((model.electrician ? "true" : "false"))\"," + addressTypeStr + "}"
        }
        
        return resultStr
    }
}
